<?php
/*------------------------------------------------------ */
//-- 快递100接口
//-- @author iqgmy
/*------------------------------------------------------ */
namespace shipping;
$i = (isset($modules)) ? count($modules) : 0;
$modules[$i]["name"] = "快递100";
class Kd100{
     /*------------------------------------------------------ */
	//--  获取物流信息
	/*------------------------------------------------------ */
	public static function getLog($shipping_code,$invoice_no,$mobile=''){
		$key = settings('kd100_apikey');	   //客户授权key
		$customer = settings('kd100_customer');  //查询公司编号
		$param = array (
			'com' => $shipping_code,// $orderInfo['shipping_code'], //快递公司编码
			'num' => $invoice_no,// $orderInfo['invoice_no'],	  //快递单号
			'phone' => $mobile,//18988914414,//$orderInfo['mobile'],// $orderInfo['mobile'],      //手机号
			'resultv2' => '1'			//开启行政区域解析
		);
		//请求参数

    	$post_data = array();
    	$post_data["customer"] = $customer;
    	$post_data["param"] = json_encode($param);
    	$sign = md5($post_data["param"].$key.$post_data["customer"]);
    	$post_data["sign"] = strtoupper($sign);
    	$url = 'http://poll.kuaidi100.com/poll/query.do';	//实时查询请求地址
		$params = "";
		foreach ($post_data as $k=>$v) {
	        $params .= "$k=".urlencode($v)."&";		//默认UTF-8编码格式
	    }
	    $post_data = substr($params, 0, -1);


		$data = self::vget($url,$post_data);

		if ($data['status'] != 200) {
			$return['code'] = 0;
			$return['msg'] = $data['message'];
			return $return;
		}

		$return['code'] = 1;
		$return['data'] = $data['data'];
		return $return;
	}
	 /*------------------------------------------------------ */
	//--  数据请求
	/*------------------------------------------------------ */
	public static function vget($url,$data){ // 模拟获取内容函数
		  $header = array(
					'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
					'Accept-Encoding:gzip, deflate',
					'Accept-Language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3',
					'Connection:keep-alive',
					'poll.kuaidi100.com',
			);
		   	$useragent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0';
			$curl = curl_init(); // 启动一个CURL会话
			curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
			curl_setopt($curl, CURLOPT_HTTPHEADER, $header); //设置HTTP头字段的数组
			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
			curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
			curl_setopt($curl, CURLOPT_USERAGENT, $useragent); // 模拟用户使用的浏览器
			curl_setopt($curl,CURLOPT_POSTFIELDS,$data); //发送POST数据
			curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
			curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
			curl_setopt($curl, CURLOPT_HTTPGET, 1); // 发送一个常规的GET请求
			curl_setopt($curl, CURLOPT_COOKIE, 'Hm_lvt_22ea01af58ba2be0fec7c11b25e88e6c=1555330368; WWWID=WWWF5A988734DE61FC84FC0BF04746AE8DD; Hm_lpvt_22ea01af58ba2be0fec7c11b25e88e6c=1555989068; '); // 读取上面所储存的Cookie信息
			curl_setopt($curl, CURLOPT_ENCODING, "gzip"); // 关键在这里
			curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
			curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
			curl_setopt($curl, CURLOPT_POST, 1);
			$tmpInfo = curl_exec($curl); // 执行操作
			if (curl_errno($curl)) {
					// echo 'Errno'.curl_error($curl);
			}
			curl_close($curl); // 关闭CURL会话
			return json_decode($tmpInfo,true); // 返回数据
	}

	public static function curl_post($url,$post_data,$timeout = 30){
		if($url == "" || $timeout <= 0){
			return false;

		}

		$post_data = http_build_query($post_data);//重点

		$curl = curl_init();

		curl_setopt($curl, CURLOPT_URL, $url);

		curl_setopt($curl, CURLOPT_HEADER, 0);//不抓取头部信息。只返回数据

		curl_setopt($curl, CURLOPT_TIMEOUT, (int)$timeout);//超时设置

		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//1表示不返回bool值

		curl_setopt($curl, CURLOPT_POST, 1);

		curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));//重点

		curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);

		$data = curl_exec($curl);

		if (curl_errno($curl)) {
			return curl_error($curl);

		}

		curl_close($curl);
		return  $data;
	}

}


?>
